package com.wiegand.gui.text.effects {

	/**
	 * @author wie-gand
	 */
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;

	public class RainbowTextField extends TextField{

		private var _timer : Timer;
		private var _position : int = 0;			
		private var _delay : int = 60;
		private var _text : String = "";
		
		private var _tfStart : TextFormat;
		private var _tfEnd : TextFormat;
		private var _transitionTfs : Array;
				
		public function RainbowTextField( startTextFormat : TextFormat = null, endTextFormat : TextFormat = null, transitions : Array = null ) : void{
			
			super();
			
			if( startTextFormat != null ) _tfStart = startTextFormat;
			else{
				
				_tfStart = new TextFormat();
				_tfStart.size = 20;
				_tfStart.color = 0xaaaaaa;
				_tfStart.font = "Verdana";
			
			}
			
			if( endTextFormat != null ) _tfEnd = endTextFormat;
			else{
				
				_tfEnd = new TextFormat();
				_tfEnd.size = 30;
				_tfEnd.color = 0x333333;
				_tfEnd.font = "Verdana";
			
			}			
			
			if( transitions != null ) _transitionTfs = transitions;
			else _transitionTfs = new Array();
			
			_timer = new Timer( _delay );
			
			_timer.addEventListener( TimerEvent.TIMER, timerHandler );
			
		}
		
		public function get transitions() : Array{
			
			return _transitionTfs;
			
		}
		
		public function set transitions( value : Array ) : void{
			
			_transitionTfs = value;
			
		}
		
		public function get startTextFormat() : TextFormat{
			
			return _tfStart;
			
		}
		
		public function set startTextFormat( value : TextFormat ) : void{
			
			_tfStart = value;
			
		}
		
		public function get endTextFormat() : TextFormat{
			
			return _tfEnd;
			
		}
		
		public function set endTextFormat( value : TextFormat ) : void{
			
			_tfEnd = value;
			
		}
		
		public function get delay() : int{
			
			return _delay;
			
		}
		
		public function set delay( value : int ) : void{
			
			_delay = value;
			
		}

		override public function get text() : String{
			
			return _text;
		
		}
		
		override public function set text( value : String ) : void{
			
			super.text = "";
					
			_text = value;
			
			super.text = _text;	
			
			setTextFormat( _tfStart );
			
			if( value.length > 0 ){
				
				_timer.delay = _delay;
				
				_timer.start();
				
			}
		}
		
		private function timerHandler( e : TimerEvent ) : void{
					
			
			
//			for( var i : int = 0; i < _transitionTfs.length ; i++ ){
//			
//				setTextFormat( _transitionTfs[ i ], 0, _position + ( i ) );
//			
//			}
			
			
//			for( var j : int = 0; j < _transitionTfs.length ; j++ ){
//			
//				setTextFormat( _transitionTfs[ _transitionTfs.length + j ], 0, _position + ( _transitionTfs.length + j + 1 ) );
//			
//			}
//			
			
			if( ( _position + _transitionTfs.length + 1 ) <= text.length  ) setTextFormat( _tfEnd, 0, _position + _transitionTfs.length + 1 );
			
			
			
			
			
			if( ( _position + _transitionTfs.length ) <= text.length  ) setTextFormat( _tfStart, 0, _position + _transitionTfs.length );
			
//		
//		
			
			_position ++;
			
//			if( _position > text.length + ( _transitionTfs.length * 2 + 1 ) ) _position = 0;

//			mytext.setTextFormat(0,di,transition3);
//			mytext.setTextFormat(0,di-1,transition2);
//			mytext.setTextFormat(0,di-2,transition1);
//			mytext.setTextFormat(0,di-3,white);
//			mytext.setTextFormat(0,di-4,transition1);
//			mytext.setTextFormat(0,di-5,transition2);
//			mytext.setTextFormat(0,di-6,transition3);
//			mytext.setTextFormat(0,di-7,simple);
//			di++;
//			if(di>mytext.length+7){
//			di=0;
//			updateAfterEvent();
//			}


		
		}
	
	}
	
}